<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools :title="`共${{total}}条数据`">
        <el-button type="danger" size="small">普通excel导出</el-button>
        <el-button type="info" size="small">复杂表头Excel导出</el-button>
        <el-button type="success" size="small">Excel导入</el-button>
        <el-button type="primary" size="small" @click="showDialog = true">新增员工</el-button>
      </PageTools>
      <!-- 表格 -->
      <el-table :data="list" border>
        <el-form-item type="index" label="序号" />
        <el-form-item label="姓名" prop="username" />
        <el-form-item label="头像" prop="staffPhoto" />
        <el-form-item label="手机号" prop="mobile" />
        <el-form-item sortable label="工号" prop="workNumber" />
        <!-- <el-form-item label="聘用形式" prop="formOfEmployment">
          <template slot-scope="scope">
            <span v-if="scope.row.formOfEmployment*1===1">正式</span>
            <span v-if="scope.row.formOfEmployment*1===2">非正式</span>
          </template> -->
        <el-form-item label="聘用形式" :formatter="formatterFn" prop="formOfEmployment">
          <el-select v-model="formOfEmployment">
            <el-option
              v-for="item in hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-cascader
            v-model="formData.departmentName"
            :options="departs"
            :props="{ label:'name', value: 'name' }"
          />
        </el-form-item>
        <el-form-item sortable label="入职时间" prop="timeOfEntry">
          <el-date-picker v-model="formData.timeOfEntry" type="date" placeholder="选择日期时间" />
        </el-form-item>
        <el-form-item label="转正时间">
          <el-date-picker v-model="formData.correctionTime" type="date" placeholder="选择日期" />
        </el-form-item>
        <el-form-item label="操作" width="300px">
          <template slot-scope="scope">
            <el-button type="text">查看</el-button>
            <el-button type="text">转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button type="text">角色</el-button>
            <el-button type="text" @click="delHandler(scope.row.id)">删除</el-button>
          </template>
        </el-form-item>
      </el-table>
      <!-- 分页 -->
      <!-- 分页组件 -->
      <el-row type="flex" align="middle" style="height: 60px">
        <!-- 分页组件 -->
        <el-pagination
          :current-page="queryData.page"
          :page-sizes="[4, 5, 6, 7]"
          :page-size="queryData.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
    <!-- 新增弹框 -->
    <el-dialog title=" 新增员工 " :visible.sync="showDialog" width="50%">
      <!--表单  -->
      <el-form ref="form" :model="formData" :rules="formRules" label-width="180px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="formData.username" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="formData.mobile" />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-input v-model="formData.timeOfEntry" />
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-input v-model="formData.formOfEmployment" />
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="formData.workNumber" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input v-model="formData.departmentName" />
        </el-form-item>
        <el-form-item label="转正时间">
          <el-input v-model="formData.correctionTime" />
        </el-form-item>
      </el-form>
      <!-- 底部插槽 -->
      <span slot="footer">
        <el-button @click="showDialog = false">Cancel</el-button>
        <el-button type="primary">OK</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { getUserListData, delUserData } from '@/api/employees'
import employeesConstant from '@/api/constant/employees'
import { getDepartmentListData } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      queryData: {
        page: 1,
        size: 10
      },
      total: 0,
      list: [],
      hireType: employeesConstant.hireType,
      // 弹框控制变量
      showDialog: false,
      // 表单变量
      formData: {},
      // 表单规则对象
      formRules: {
        username: [{ required: true, message: '请输入', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '请输入', trigger: 'blur' }],
        formOfEmployment: [{ required: true, message: '请输入', trigger: 'blur' }],
        workNumber: [{ required: true, message: '请输入', trigger: 'blur' }],
        departmentName: [{ required: true, type: 'array', message: '请输入', trigger: 'blur' }]
      },
      departs: []
    }
  },
  created() {
    this.initData()
    this.getDeptsData()
  },
  methods: {
    async initData() {
      // 发起请求
      const { rows, total } = await getUserListData(this.queryData)
      // 赋值
      this.list = rows
      this.total = total
    },
    async getDeptsData() {
      const { depts } = await getDepartmentListData()
      this.departs = tranListToTreeData(depts, '')
    },
    handleSizeChange(val) {
      // val是每页最新数量
      this.queryData.pagesize = val
      this.initData()
    },
    handleCurrentChange(val) {
      // val是每页最新的页码
      this.queryData.page = val
      this.initData()
    },
    // 列表格式化函数
    formatterFn(row, column, cellValue, index) {
      const res = this.hireType.find(item => item.id === cellValue * 1)
      return res ? res.value : '未知'
    },
    // 删除
    async delHandler(id) {
      await this.$confirm('确认删除吗?')
      await delUserData(id)
      this.$message.success('删除成功')
      if (this.queryData.page !== 1 && this.list.length === 1) {
        this.queryData.page--
      }
      this.initData()
    },
    // 添加员工
    addBtn: {

    }
  }
}
</script>

<style lang="scss" scoped>

</style>
